#include "appimage.h"
#include <QProcess>
#include <QDir>
#include <QDebug>
#include <QFileInfo>
#include <QFile>
#include <QCoreApplication>
#include <iostream>
#include <QMimeDatabase>
using namespace std;
appimage::appimage()
{

}

void appimage::setFilename(QString Filename)
{
    fileName=Filename;
}

void appimage::load()
{
    QFileInfo appinfo(fileName);
    QFile list(QCoreApplication::applicationDirPath()+"/data/list");
    QString listline;
    QStringList line_ary;//# 0标记|1名字|2图标|3分类
    if(fileName==""){
        qDebug()<<"Error: Filename is empty!";
        return;
    }{

        QDir::temp().mkdir("appimage-installer");
        QDir::setCurrent(QDir::tempPath()+"/appimage-installer");
        fileName="\""+fileName+"\"";

        QProcess unpack;
        unpack.start("rm -rf "+QDir::temp().path()+"/appimage-installer/squashfs-root");
        unpack.waitForFinished();
        unpack.start("fusermount -u "+ QDir::temp().path()+"/appimage-installer/squashfs-root");
        unpack.waitForFinished();
        //判断类型
        QMimeDatabase db;
        if(db.mimeTypeForFile(appinfo,QMimeDatabase::MatchContent).name()=="application/vnd.appimage"){
            QProcess chmod;
            chmod.start("chmod +x "+fileName);
            chmod.waitForFinished();
            unpack.start(fileName + " --appimage-extract");
            unpack.waitForFinished();
//            unpack.start("7z X -o\""+QDir::temp().path()+"/appimage-installer/squashfs-root\" "+fileName);
//            unpack.waitForFinished();
//            qDebug()<<"unpack:"<<"7z X -o"+QDir::temp().path()+"/appimage-installer/squashfs-root\" "+fileName;
            qDebug()<<"out:"<<unpack.readAll();

        }else if (db.mimeTypeForFile(appinfo,QMimeDatabase::MatchContent).name()=="application/x-iso9660-appimage") {
            unpack.start("fuseiso -p "+ fileName + " \"" +QDir::temp().path()+"/appimage-installer/squashfs-root\"");
            unpack.waitForFinished();
            qDebug()<<"out:"<<unpack.readAll();
        }
        //获取信息
        appinfo.fileName();
        list.open(QIODevice::ReadOnly | QIODevice::Text);
        while (!list.atEnd()) {
            listline=list.readLine();
            if(listline.left(1)!="#"){
                line_ary=listline.split("|");
                qDebug()<< line_ary;
                if(line_ary[0].length()<=appinfo.fileName().length()){
                    if(appinfo.fileName().left(line_ary[0].length())==line_ary[0]){
                        //这说明list中找到了标记
                        qDebug()<<appinfo.fileName().left(line_ary[0].toInt());
                        name=line_ary[1];
                        themeicon=line_ary[2];
                        type=line_ary[3];
                        isdefault_=true;
                        break;
                    }else {
                        isdefault_=false;
                    }
                }


            }


        }
    }

}


bool appimage::isdefault()
{
    return isdefault_;
}
